package com.sc.nft.service.impl;

import com.google.common.collect.Lists;
import com.sc.nft.dao.MallMemberEquityTargetDao;
import com.sc.nft.entity.MallMemberEquityTarget;
import com.sc.nft.entity.dto.CheckHasPreemptionGetByTargetDTO;
import com.sc.nft.entity.vo.MallMemberEquityTargetVO;
import com.sc.nft.service.MallMemberEquityTargetService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商城会员权益对应福利 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-02
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MallMemberEquityTargetServiceImpl implements MallMemberEquityTargetService {

    private final MallMemberEquityTargetDao mallMemberEquityTargetDao;

    /**
     * 添加或者编辑商城会员权益对应福利
     *
     * @param mallMemberEquityId
     * @param mallMemberEquityTargetVOList
     */
    @Override
    public void add(Long mallMemberEquityId, List<MallMemberEquityTargetVO> mallMemberEquityTargetVOList) {
        List<MallMemberEquityTarget> mallMemberEquityTargets = Lists.newArrayList();
        for (MallMemberEquityTargetVO mallMemberEquityTargetVO : mallMemberEquityTargetVOList) {
            MallMemberEquityTarget mallMemberEquityTarget = new MallMemberEquityTarget();
            mallMemberEquityTarget.setMallMemberEquityId(mallMemberEquityId);
            mallMemberEquityTarget.setTargetId(mallMemberEquityTargetVO.getTargetId());
            mallMemberEquityTarget.setNum(mallMemberEquityTargetVO.getNum());
            mallMemberEquityTarget.setCan_use_num(mallMemberEquityTargetVO.getNum());
            mallMemberEquityTargets.add(mallMemberEquityTarget);
        }
        mallMemberEquityTargetDao.saveBatch(mallMemberEquityTargets);
    }

    /**
     * 添加或者编辑商城会员权益对应福利
     *
     * @param mallMemberEquityId
     * @param mallMemberEquityTargetVOList
     */
    @Override
    public void update(Long mallMemberEquityId, List<MallMemberEquityTargetVO> mallMemberEquityTargetVOList) {
        List<MallMemberEquityTarget> mallMemberEquityTargetList = getListById(mallMemberEquityId);
        Map<Long, Integer> mallActivityShareSpecsMap = mallMemberEquityTargetList.stream().collect(Collectors.toMap(MallMemberEquityTarget::getTargetId, MallMemberEquityTarget::getNum, (value1, value2) -> value1));
        for (MallMemberEquityTargetVO mallMemberEquityTargetVO : mallMemberEquityTargetVOList) {
            Long targetId = mallMemberEquityTargetVO.getTargetId();
            Integer num = mallMemberEquityTargetVO.getNum();
            if (mallActivityShareSpecsMap.containsKey(targetId)) {
                Integer oldNum = mallActivityShareSpecsMap.get(targetId);
                if (!oldNum.equals(num)) {
                    mallMemberEquityTargetDao.updateNumAndCanUseNum(mallMemberEquityId, targetId, num);
                }
            } else {
                MallMemberEquityTarget mallMemberEquityTarget = new MallMemberEquityTarget();
                mallMemberEquityTarget.setMallMemberEquityId(mallMemberEquityId);
                mallMemberEquityTarget.setTargetId(targetId);
                mallMemberEquityTarget.setNum(num);
                mallMemberEquityTarget.setCan_use_num(num);
                mallMemberEquityTargetDao.save(mallMemberEquityTarget);
            }
        }
        List<Long> targetIds = mallMemberEquityTargetVOList.stream().map(MallMemberEquityTargetVO::getTargetId).collect(Collectors.toList());
        for (MallMemberEquityTarget mallMemberEquityTarget : mallMemberEquityTargetList) {
            if (!targetIds.contains(mallMemberEquityTarget.getTargetId())) {
                mallMemberEquityTargetDao.removeById(mallMemberEquityTarget.getId());
            }
        }
    }

    /**
     * 通过会员权益id获取
     *
     * @param mallMemberEquityId
     * @return
     */
    @Override
    public List<MallMemberEquityTarget> getListById(Long mallMemberEquityId) {
        return mallMemberEquityTargetDao.getList(mallMemberEquityId);
    }

    /**
     * 移除会员权益对应福利
     *
     * @param mallMemberEquityId
     */
    @Override
    public void remove(Long mallMemberEquityId) {
        mallMemberEquityTargetDao.remove(mallMemberEquityId);
    }

    /**
     * 优先抢查询当前商品规格是否存在优先抢
     *
     * @param activitySpecId
     * @return
     */
    @Override
    public List<CheckHasPreemptionGetByTargetDTO> checkHasPreemptionGetByTargetId(Long activitySpecId) {
        return mallMemberEquityTargetDao.checkHasPreemptionGetByTargetId(activitySpecId);
    }
}
